/**
 * 
 */
package dao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import bean.Student;

/**
 * 
 * @author liuxing
 *
 */
@Repository
public class StudentDao {

	@Resource
	private MongoTemplate mongoTemplate;
	
	/**
	 * 查詢所有
	 * @return
	 */
	public List<Student> getStudentList(){
		
//		List<Student> studentList = mongoTemplate.find(null, Student.class, "t_user");
//		List<Student> studentList = mongoTemplate.find(null,Student.class, "t_user");
		List<Student> studentList = mongoTemplate.findAll(Student.class, "t_user");
		
		return studentList;
	}
	
	/**
	 * 单条件查询
	 * @param name
	 * @return
	 */
	public List<Student> getStudentList(String name){
		
		// 查询条件：用户姓名
		Criteria criteria = Criteria.where("user_name").is(name);
		
		Query query = new Query(criteria);
		
		List<Student> studentList = mongoTemplate.find(query, Student.class, "t_user");
		
		return studentList;
	}
	
	/**
	 * 多条件查询
	 * @param id
	 * @param name
	 * @return
	 */
	public List<Student> getStudentList(Double id, String name){
		
		// 查询条件：用户ID大于参数id，并且用户姓名为参数name
		Criteria criteria = Criteria.where("user_id").gt(id).and("user_name").is(name);
		
		Query query = new Query(criteria);
		
		List<Student> studentList = mongoTemplate.find(query, Student.class, "t_user");
		
		return studentList;
	}

	/**
	 * 模糊查询
	 * @param name
	 * @return
	 */
	public List<Student> getStudentListWithSingleFuzzy(String name){
		
		// 查询条件：用户ID大于参数1，并且用户姓名为参数2
		Criteria criteria = Criteria.where("user_name").regex(name);
		
		Query query = new Query(criteria);
		
		List<Student> studentList = mongoTemplate.find(query, Student.class, "t_user");
		
		return studentList;
	}
	
	/**
	 * 带别的条件的模糊查询
	 * @param student
	 * @return
	 */
	public List<Student> getStudentListWithMultiFuzzy(Student student){
		
		// 查询条件：用户姓名模糊匹配[参数1]，并且用户ID大于[参数2]
		Criteria criteria = Criteria.where("user_name").regex(student.getUser_name()).and("user_id").gt(student.getUser_id());
		
		Query query = new Query(criteria);
		
		List<Student> studentList = mongoTemplate.find(query, Student.class, "t_user");
		
		return studentList;
	}
	
	/**
	 * 无条件的分页
	 * @param page
	 * @param pageSize
	 * @return
	 */
	public List<Student> getStudentListWithPaging(int page, int pageSize){
		
		Query query = new Query();
		
		// 设置分页
		query.skip((page - 1) * pageSize).limit(pageSize);
		
		List<Student> studentList = mongoTemplate.find(query, Student.class, "t_user");
		
		return studentList;
	}
	
	/**
	 * 带条件的分页
	 * @param page
	 * @param pageSize
	 * @param name
	 * @return
	 */
	public List<Student> getStudentListWithPaging(int page, int pageSize, String name){
		
		// 查询条件：用户ID大于参数1，并且用户姓名为参数2
		Criteria criteria = Criteria.where("user_name").regex(name);
		
		Query query = new Query(criteria);
		
		// 设置分页
		query.skip((page - 1) * pageSize).limit(pageSize);
		
		List<Student> studentList = mongoTemplate.find(query, Student.class, "t_user");
		
		return studentList;
	}
	
	/**
	 * 单字段排序
	 * @param direction
	 * @return
	 */
	public List<Student> getStudentListWithSingleSort(Direction direction){
		
		Query query = new Query();
		
		// 设置排序
		Sort sort = new Sort(direction, "user_id");
		query.with(sort);
		
		List<Student> studentList = mongoTemplate.find(query, Student.class, "t_user");
		
		return studentList;
	}
	
	/**
	 * 多字段排序
	 * @param direction
	 * @return
	 */
	public List<Student> getStudentListWithMultiSort(Direction direction){
		
//		Direction.ASC
		
		Query query = new Query();
		
		// 设置多个排序字段
		Sort sort = new Sort(direction, "user_id").and(new Sort(direction, "user_name"));
		query.with(sort);
		
		List<Student> studentList = mongoTemplate.find(query, Student.class, "t_user");
		
		return studentList;
	}
	
	/**
	 * 带条件的排序
	 * @param direction
	 * @param name
	 * @return
	 */
	public List<Student> getStudentListWithSingleSort(Direction direction, String name){
		
		// 查询条件：用户姓名中包含参数[name]
		Criteria criteria = Criteria.where("user_name").regex(name);
		
		Query query = new Query(criteria);
		
		// 设置排序
		Sort sort = new Sort(direction, "user_id");
		query.with(sort);
		
		List<Student> studentList = mongoTemplate.find(query, Student.class, "t_user");
		
		return studentList;
	}
	
	/**
	 * 插入
	 * @param student
	 */
	public void insert(Student student){

		mongoTemplate.insert(student, "t_user");
	}
	
	/**
	 * 更新单个记录
	 * @param student
	 */
	public void update(Student student){

		// 更新条件
		Criteria criteria = Criteria.where("user_id").is(student.getUser_id());
		Query query = new Query(criteria);
		
		// 更新内容
		Update update = new Update();
		update.set("user_name", student.getUser_name());
		
		mongoTemplate.updateFirst(query, update, "t_user");
	}
	
	/**
	 * 方法描述：更新多条记录
	 */
	public void updateBatch(Student student){

		// 更新条件：用户ID >= [参数]
		Criteria criteria = Criteria.where("user_id").gte(student.getUser_id());
		Query query = new Query(criteria);
		
		// 更新内容：用户姓名 = [参数]
		Update update = new Update();
		update.set("user_name", student.getUser_name());
		
		mongoTemplate.updateMulti(query, update, "t_user");
	}
	
	/**
	 * 方法描述：删除符合条件的记录
	 * @param student
	 */
	public void delete(Student student){

		// 删除条件：用户ID = [参数]
		Criteria criteria = Criteria.where("user_id").is(student.getUser_id());
		Query query = new Query(criteria);
		
		mongoTemplate.remove(query, "t_user");
	}
	
}
